// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import MainControl from "./mainControl"

@ccclass
export default class BirdControl extends cc.Component {

    // 展示分数
    @property(cc.Label)
    score: cc.Label = null;
    @property
    grade: number = 0;

    // 播放的动画名称
    @property
    anim: string = null;

    @property(cc.Animation)
    animation: cc.Animation = null;

    // 公共节点
    @property(MainControl)
    mainControl: MainControl = null;

    onLoad() {
        // 开启物理引擎
        cc.director.getPhysicsManager().enabled = true;
    }

    start() {
        this.mainControl = cc.find("persistentNode").getComponent("mainControl");

        this.animation = this.getComponent(cc.Animation);
        this.animation.on("finished", this.finishedAnim, this);

    }

    playAnim(name) {
        // if (this.anim == name) return;
        this.anim = name;
        this.animation.play(this.anim);
    }

    finishedAnim(e, data) {
        this.anim = null;
    }

    // 供外部调用
    fly() {
        this.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, 200)
        this.playAnim('bird')
    }

    update (dt) {}
    onBeginContact(contact, self, other) {
        if (other.tag == 1) {
            this.grade++;
            this.score.string = this.grade + '';
        } else {
            this.mainControl.gameOver(this.grade);
        }
    }
}
